#include <QDateTime>
#include <QDebug>
#include <QFile>
#include <QMessageBox>


#include "coder.h"


coderClass::coderClass()
{

}

void coderClass::pushCode(dbcClass &dbc, QString path)
{
    QDateTime cur_time =QDateTime::currentDateTime();
    QString cur_time_str =cur_time.toString("yyyyMMddhhmmss");
    qDebug() << cur_time_str;
    pushCode_h(dbc,path,cur_time_str);
    pushCode_c(dbc,path,cur_time_str);
    QMessageBox::information(nullptr, "", "搞定了");
}

void coderClass::pushCode_h(dbcClass &dbc,QString path,QString &stamp)
{
    QString hfileName = path;
    hfileName.append("/code");
    hfileName.append(stamp);
    hfileName.append(".h");
    qDebug() << "h code file name " << hfileName;

    QFile hfile(hfileName);        // 生成的c代码
    if(false == hfile.open(QIODevice::ReadWrite|QIODevice::Text|QIODevice::Truncate))
    {
        QMessageBox::information(nullptr, "ERROR", "h文件打开失败");
        qDebug() << "h文件打开失败";
    }
    qDebug() << "h文件打开成功";

    hfile.flush();
    QTextStream h(&hfile);

    int i,j;
    DBcMessage_t *msg;
    DBcSignal_t  *sig;
    qint64 rightEndValue,leftEndValue, maxValue;

    QString type;
    h << endl;
    h << "#include <stdint.h>" << endl;
    h << endl;
    h << endl;
    for(i=0; i<dbc.msgList.size(); i++)
    {
        h << "typedef struct" << endl;
        h << "{" << endl;
        msg = &(dbc.msgList[i]);
        for(j=0; j<msg->sigList.size(); j++)
        {
           sig = &(msg->sigList[j]);
           if(signedOrUnsigned != sig->exVauleType)
           {
               type = "double  ";
           }
           else   //(signedOrUnsigned == sig->exVauleType)
           {
               if(isFloatValue(sig->factor) || isFloatValue(sig->offset))    // factor 或 offset是浮点数
               {
                    type = "double  ";
               }
               else        // factor 和 offset 都是整数
               {
                   if(unsignedType == sig->valueType)
                   {
                       rightEndValue = static_cast<qint64>(((0x01 << (sig->bitSize))-1)   * sig->factor + sig->offset);  // 最右端值限值
                       leftEndValue  = static_cast<qint64>(sig->offset);  // 最左端值限值
                   }
                   else //(signedType == sig->valueType)
                   {
                       rightEndValue = static_cast<qint64>(((0x01 << (sig->bitSize - 1)) - 1)      * sig->factor + sig->offset);  // 最右端值限值
                       leftEndValue  = static_cast<qint64>(((0x01 << (sig->bitSize - 1)) * (-1))   * sig->factor + sig->offset);  // 最左端值限值
                   }
                   if((rightEndValue >= 0) && (leftEndValue >= 0))    // 两端的值都大于等于0
                   {
                      maxValue = this->max(rightEndValue,leftEndValue);
                      if(maxValue <= UINT8_MAX)       {type = "uint8_t ";}
                      else if(maxValue <= UINT16_MAX) {type = "uint16_t";}
                      else if(maxValue <= UINT32_MAX) {type = "uint32_t";}
                      else                            {type = "uint64_t";}
                   }
                   else
                   {
                       if((INT8_MIN <= rightEndValue) && (rightEndValue <= INT8_MAX)
                               && (INT8_MIN <= leftEndValue) && (leftEndValue <= INT8_MAX))
                       {type = "int8_t ";}

                       else if((INT16_MIN <= rightEndValue) && (rightEndValue <= INT16_MAX)
                               && (INT16_MIN <= leftEndValue) && (leftEndValue <= INT16_MAX))
                       { type = "int16_t";}

                       else if((INT32_MIN <= rightEndValue) && (rightEndValue <= INT32_MAX)
                               && (INT32_MIN <= leftEndValue) && (leftEndValue <= INT32_MAX))
                       {type = "int32_t";}

                       else
                       {type = "int64_t";}
                   }
               }
           }
           h << "    " << type << " "<< sig->name << ";" << endl;
        }
        h << "}" << msg->header.name << "_t;" << endl;
        h << endl;
        qDebug() << msg->header.name << "  " <<msg->header.id << "pushed " ;
    }
    for(i=0; i<dbc.msgList.size(); i++)
    {
        msg = &(dbc.msgList[i]);
        h << "void " << "msg0x" << QString::number(msg->header.id,16).toUpper();
        h << "_" << msg->header.name << "_decode(uint8_t data[], " << msg->header.name << "_t *msg );" << endl;
        h << "void " << "msg0x" << QString::number(msg->header.id,16).toUpper();
        h << "_" << msg->header.name << "_encode(uint8_t data[], " << msg->header.name << "_t *msg );" << endl;
    }
    qDebug() << "h code pushed ";
    hfile.close();
}

void coderClass::pushCode_c(dbcClass &dbc, QString path,QString &stamp)
{
    QString cfileName = path;

    cfileName.append("/code");
    cfileName.append(stamp);
    cfileName.append(".c");

    QString hfileName = "code";
    hfileName.append(stamp);
    hfileName.append(".h");

    qDebug() << "c code file name " << cfileName;

    QFile cfile(cfileName);
    if(false == cfile.open(QIODevice::ReadWrite|QIODevice::Text|QIODevice::Truncate))
    {
        QMessageBox::information(nullptr, "ERROR", "c文件打开失败");
        qDebug() << "c文件打开失败";
    }
    cfile.flush();
    QTextStream c(&cfile);

    int i,j;
    quint32 startByte      = 0;     // 从哪个字节开始
    quint32 byteLength     = 0;     // 字节长度
    quint32 lsbPosInByte   = 0;     // 最低位在其字节中的位置
    quint32 startBitInByte = 0;     // 在某个字节里的起始位
    quint32 ibyte;
    quint32 shiftCnt;
    quint64 mask, mask_full,mask_inv;
    DBcMessage_t *msg;
    DBcSignal_t  *sig;
    QString tempValueStr;
    QString tempValueStr_s;

    c << "#include " << "\"" << hfileName << "\"" << endl;
    c << endl;
    c << "static int64_t u2s_64(uint64_t data,uint8_t n);" << endl;
    c << "static int32_t u2s_32(uint32_t data,uint8_t n);" << endl;
    c << "static int16_t u2s_16(uint16_t data,uint8_t n);" << endl;
    c << "static int8_t  u2s_8(uint8_t data,uint8_t n);" << endl;
    c << endl;
    c << endl;
    c << "static union{      " << endl;
    c << "    uint8_t u[4]; " << endl;
    c << "    float   f;    " << endl;
    c << "}temp_uf;         " << endl;
    c << endl;
    c << "static union{     " << endl;
    c << "    uint8_t u[8]; " << endl;
    c << "    double  d;    " << endl;
    c << "}temp_ud;         " << endl;
    c << endl;
    c << "static union{     " << endl;
    c << "    uint8_t u;    " << endl;
    c << "    int8_t  s;    " << endl;
    c << "}temp_us8;        " << endl;
    c << endl;
    c << "static union{     " << endl;
    c << "    uint16_t u;   " << endl;
    c << "    int16_t  s;   " << endl;
    c << "}temp_us16;       " << endl;
    c << endl;
    c << "static union{     " << endl;
    c << "    uint32_t u;   " << endl;
    c << "    int32_t  s;   " << endl;
    c << "}temp_us32;       " << endl;
    c << endl;
    c << "static union{     " << endl;
    c << "    uint64_t u;   " << endl;
    c << "    int64_t  s;   " << endl;
    c << "}temp_us64;       " << endl;
    c << endl;

    c << "static uint8_t  temp_u8;"  << endl;
    c << "static uint16_t temp_u16;" << endl;
    c << "static uint32_t temp_u32;" << endl;
    c << "static uint32_t temp_u64;" << endl;
    c << endl;
    c << endl;
    for(i=0; i<dbc.msgList.size(); i++)
    {
        msg = &(dbc.msgList[i]);

        c << "void " << "msg0x" << QString::number(msg->header.id,16).toUpper();
        c << "_" << msg->header.name << "_decode(uint8_t data[], " << msg->header.name << "_t *msg )" << endl;
        c << "{" << endl;

        for(j=0; j<msg->sigList.size(); j++)
        {
           sig = &(msg->sigList[j]);
           startByte  = sig->startBit / 8;       // 从哪个字节开始
           startBitInByte = sig->startBit % 8;

           if( signedOrUnsigned == sig->exVauleType)
           {
               c << "    ";
               c << "msg->" << sig->name << " = ";
               if((signedType == sig->valueType) && (sig->bitSize >= 2))
               {
                   if(sig->bitSize <= 8)      { c << "u2s_8(";  }
                   else if(sig->bitSize <= 16){ c << "u2s_16("; }
                   else if(sig->bitSize <= 32){ c << "u2s_32("; }
                   else if(sig->bitSize <= 64){ c << "u2s_64("; }
               }
               c << "(";/************************* factor */
               c << endl;
               c << "          ";
               c << "(";     /****** 多字节  ***/
               c << "(";      /****** mask ***/
               c << "data[" ;
               if( bigEndian == sig->byteOrder)      // 大端字节序，Motorola
               {
                  byteLength = (8-1-startBitInByte+ sig->bitSize + 7) / 8;  // 字节长度
                  lsbPosInByte = (8 - ((sig->bitSize - (sig->startBit % 8 + 1)) %8)) % 8;
                  c << (startByte+byteLength-1) << "]" ;
                  c << " >> " << lsbPosInByte;
                  c << ")";  /****** mask **/
                  c << " & 0x";
                  shiftCnt = 8 - lsbPosInByte;
                  shiftCnt = (shiftCnt > sig->bitSize)? sig->bitSize : shiftCnt;
                  mask = (0x01 << shiftCnt) - 1;
                  c << QString::number(mask,16);
                  c << ")";                              /***** 多字节 **/
                  for(ibyte=1; ibyte<byteLength; ibyte++)
                  {
                      c << endl;
                      c << "        ";
                      c << "| ((data[" << startByte+byteLength-1-ibyte << "]";
                      c << " << " << ibyte*8-lsbPosInByte << ") & 0x";
                      shiftCnt = ibyte * 8 + 8 -lsbPosInByte;

                      shiftCnt = (shiftCnt > sig->bitSize)? sig->bitSize : shiftCnt;
                       mask_full = (0x01 << shiftCnt) - 1;
                      shiftCnt = (ibyte-1)*8+8-lsbPosInByte;
                      mask_inv  = ~((0x01 << shiftCnt) - 1);
                      mask = mask_full & mask_inv;
                      c << QString::number(mask,16) << ")";
                  }

               }
               else     // 小端字节序，Intel
               {
                  byteLength = (startBitInByte+ sig->bitSize + 7) / 8;  // 字节长度
                  c << startByte << "]";
                  c << ">>" << startBitInByte ;
                  c << ")";   /*********** mask */
                  c << " & 0x";
                  shiftCnt = 8 - startBitInByte;
                  shiftCnt = (shiftCnt > sig->bitSize)? sig->bitSize : shiftCnt;
                  mask = (0x01 << shiftCnt) - 1;
                  c << QString::number(mask,16);
                  c << ")";   /**** 多字节 ***/
                  for(ibyte=1; ibyte<byteLength; ibyte++)
                  {
                      c << endl;
                      c << "        ";
                      c << " | ((data[" << startByte+ibyte << "]";
                      c << " << " << ibyte*8-startBitInByte << ") & 0x";
                      shiftCnt = ibyte * 8 + 8 -startBitInByte;
                      mask_inv  = ~((0x01 << (8*ibyte-startBitInByte)) - 1);
                      shiftCnt = (shiftCnt > sig->bitSize)? sig->bitSize : shiftCnt;
                      mask_full = (0x01 << shiftCnt) - 1;
                      mask = mask_full & mask_inv;
                      c << QString::number(mask,16) << ")";
                  }
               }

               c << endl;
               c << "        ";
               c << ")";     /************************* factor */
               if((signedType == sig->valueType) && (sig->bitSize >= 2))
               {
                   c << " ," << sig->bitSize << ")";
               }
               c << " * " << "(" << sig->factor << ")";
               c << " + " << "(" << sig->offset<< ")";
               c << ";" << endl;
           }
           else
           {
                if( IeeeFloat == sig->exVauleType)
                {
                    if(bigEndian == sig->byteOrder)
                    {
                        lsbPosInByte = (8 - ((sig->bitSize - (sig->startBit % 8 + 1)) %8)) % 8;
                        if(0 == lsbPosInByte)
                        {
                            c << "    temp_uf.u[0] = data[" << startByte+3 << "];" << endl;
                            c << "    temp_uf.u[1] = data[" << startByte+2 << "];" << endl;
                            c << "    temp_uf.u[2] = data[" << startByte+1 << "];" << endl;
                            c << "    temp_uf.u[3] = data[" << startByte   << "];" << endl;
                        }
                        else
                        {
                            shiftCnt = 8 - lsbPosInByte;
                            mask = (0x01 << shiftCnt) - 1;
                            c << "    temp_uf.u[0] = (data[" << startByte+4 << "] >> " << lsbPosInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_uf.u[0] |= (data[" << startByte+3 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_uf.u[1] = (data[" << startByte+3 << "] >> " << lsbPosInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_uf.u[1] |= (data[" << startByte+2 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_uf.u[2] = (data[" << startByte+2 << "] >> " << lsbPosInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_uf.u[2] |= (data[" << startByte+1 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_uf.u[3] = (data[" << startByte+1 << "] >> " << lsbPosInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_uf.u[3] |= (data[" << startByte  << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;
                        }

                    }
                    else  // 小端
                    {
                        if(0 == startBitInByte)
                        {
                            c << "    temp_uf.u[0] = data[" << startByte   << "];" << endl;
                            c << "    temp_uf.u[1] = data[" << startByte+1 << "];" << endl;
                            c << "    temp_uf.u[2] = data[" << startByte+2 << "];" << endl;
                            c << "    temp_uf.u[3] = data[" << startByte+3 << "];" << endl;
                        }
                        else
                        {
                            shiftCnt = 8 - startBitInByte;
                            mask = (0x01 << shiftCnt) -1;
                            c << "    temp_uf.u[0] = (data[" << startByte   << "] >> " << startBitInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_uf.u[0] |= (data[" << startByte+1 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_uf.u[1] = (data[" << startByte+1   << "] >> " << startBitInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_uf.u[1] |= (data[" << startByte+2 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_uf.u[2] = (data[" << startByte+2   << "] >> " << startBitInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_uf.u[2] |= (data[" << startByte+3 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_uf.u[3] = (data[" << startByte+3  << "] >> " << startBitInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_uf.u[3] |= (data[" << startByte+4 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;
                        }

                    }
                    c << "    ";
                    c << "msg->" << sig->name << " = " << "temp_uf.f";
                    c << " * " << "(" << sig->factor << ")";
                    c << " + " << "(" << sig->offset<< ")";
                    c << ";" << endl;
                }
                else  // IEEE double
                {
                    if(bigEndian == sig->byteOrder)
                    {
                        lsbPosInByte = (8 - ((sig->bitSize - (sig->startBit % 8 + 1)) %8)) % 8;
                        if(0 == lsbPosInByte)
                        {
                            c << "    temp_ud.u[0] = data[" << startByte+7 << "];" << endl;
                            c << "    temp_ud.u[1] = data[" << startByte+6 << "];" << endl;
                            c << "    temp_ud.u[2] = data[" << startByte+5 << "];" << endl;
                            c << "    temp_ud.u[3] = data[" << startByte+4 << "];" << endl;
                            c << "    temp_ud.u[4] = data[" << startByte+3 << "];" << endl;
                            c << "    temp_ud.u[5] = data[" << startByte+2 << "];" << endl;
                            c << "    temp_ud.u[6] = data[" << startByte+1 << "];" << endl;
                            c << "    temp_ud.u[7] = data[" << startByte   << "];" << endl;
                        }
                        else
                        {
                            shiftCnt = 8 - lsbPosInByte;
                            mask = (0x01 << shiftCnt) - 1;
                            c << "    temp_ud.u[0] = (data[" << startByte+8 << "] >> " << lsbPosInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_ud.u[0] |= (data[" << startByte+7 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_ud.u[1] = (data[" << startByte+7 << "] >> " << lsbPosInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_ud.u[1] |= (data[" << startByte+6 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_ud.u[2] = (data[" << startByte+6 << "] >> " << lsbPosInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_ud.u[2] |= (data[" << startByte+5 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_ud.u[3] = (data[" << startByte+5 << "] >> " << lsbPosInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_ud.u[3] |= (data[" << startByte+4 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_ud.u[4] = (data[" << startByte+4 << "] >> " << lsbPosInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_ud.u[4] |= (data[" << startByte+3 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_ud.u[5] = (data[" << startByte+3 << "] >> " << lsbPosInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_ud.u[5] |= (data[" << startByte+2 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_ud.u[6] = (data[" << startByte+2 << "] >> " << lsbPosInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_ud.u[6] |= (data[" << startByte+1 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_ud.u[7] = (data[" << startByte+1 << "] >> " << lsbPosInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_ud.u[7] |= (data[" << startByte  << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;
                        }


                    }
                    else  // 小端
                    {
                        if(0 == startBitInByte)
                        {
                            c << "    temp_ud.u[0] = data[" << startByte   << "];" << endl;
                            c << "    temp_ud.u[1] = data[" << startByte+1 << "];" << endl;
                            c << "    temp_ud.u[2] = data[" << startByte+2 << "];" << endl;
                            c << "    temp_ud.u[3] = data[" << startByte+3 << "];" << endl;
                            c << "    temp_ud.u[4] = data[" << startByte+4 << "];" << endl;
                            c << "    temp_ud.u[5] = data[" << startByte+5 << "];" << endl;
                            c << "    temp_ud.u[6] = data[" << startByte+6 << "];" << endl;
                            c << "    temp_ud.u[7] = data[" << startByte+7 << "];" << endl;
                        }
                        else
                        {
                            shiftCnt = 8 - startBitInByte;
                            mask = (0x01 << shiftCnt) -1;
                            c << "    temp_ud.u[0] = (data[" << startByte   << "] >> " << startBitInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_ud.u[0] |= (data[" << startByte+1 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_ud.u[1] = (data[" << startByte+1  << "] >> " << startBitInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_ud.u[1] |= (data[" << startByte+2 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_ud.u[2] = (data[" << startByte+2   << "] >> " << startBitInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_ud.u[2] |= (data[" << startByte+3 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_ud.u[3] = (data[" << startByte+3   << "] >> " << startBitInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_ud.u[3] |= (data[" << startByte+4 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_ud.u[4] = (data[" << startByte+4   << "] >> " << startBitInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_ud.u[4] |= (data[" << startByte+5 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_ud.u[5] = (data[" << startByte+5   << "] >> " << startBitInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_ud.u[5] |= (data[" << startByte+6 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_ud.u[6] = (data[" << startByte+6   << "] >> " << startBitInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_ud.u[6] |= (data[" << startByte+7 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                            c << "    temp_ud.u[7] = (data[" << startByte+7   << "] >> " << startBitInByte << ") & 0x";
                            c << QString::number(mask,16) << ";" << endl;
                            c << "    temp_ud.u[7] |= (data[" << startByte+8 << "] << " << shiftCnt << ") & 0x";
                            c << QString::number(((~mask)&0xff),16) << ";" << endl;

                        }

                    }
                    c << "    ";
                    c << "msg->" << sig->name << " = " << "temp_ud.d";
                    c << " * " << "(" << sig->factor << ")";
                    c << " + " << "(" << sig->offset<< ")";
                    c << ";" << endl;
                }
           }

        }
        c << "}" << endl;
        c << endl;

        c << "void " << "msg0x" << QString::number(msg->header.id,16).toUpper();
        c << "_" << msg->header.name << "_encode(uint8_t data[], " << msg->header.name << "_t *msg )" << endl;
        c << "{" << endl;
        for(j=0; j<msg->sigList.size(); j++)
        {
           sig = &(msg->sigList[j]);
           startByte  = sig->startBit / 8;       // 从哪个字节开始
           startBitInByte = sig->startBit % 8;

           if( signedOrUnsigned == sig->exVauleType)
           {
               if(unsignedType == sig->valueType)  // 无符号
               {
                   if(sig->bitSize <= 8)
                   {
                       tempValueStr = "temp_u8";
                   }
                   else if((8 < sig->bitSize) && (sig->bitSize <= 16))
                   {
                       tempValueStr = "temp_u16";
                   }
                   else if((16 < sig->bitSize) && (sig->bitSize <= 32))
                   {
                      tempValueStr = "temp_u32";
                   }
                   else
                   {
                      tempValueStr = "temp_u64";
                   }
                   c << "    " << tempValueStr << " = (msg->" << sig->name << " - (" << sig->offset << ")) / (" << sig->factor << ");" << endl;
               }
               else
               {
                   if(sig->bitSize <= 8)
                   {
                       tempValueStr   = "temp_us8.u";
                       tempValueStr_s = "temp_us8.s";
                   }
                   else if((8 < sig->bitSize) && (sig->bitSize <= 16))
                   {
                       tempValueStr   = "temp_us16.u";
                       tempValueStr_s = "temp_us16.s";
                   }
                   else if((16 < sig->bitSize) && (sig->bitSize <= 32))
                   {
                      tempValueStr   = "temp_us32.u";
                      tempValueStr_s = "temp_us32.s";
                   }
                   else
                   {
                      tempValueStr   = "temp_us64.u";
                      tempValueStr_s = "temp_us64.s";
                   }
                   c << "    " << tempValueStr_s << " = (msg->" << sig->name << " - (" << sig->offset << ")) / (" << sig->factor << ");" << endl;
               }
               if(bigEndian == sig->byteOrder)  // 大端
               {
                   byteLength = (8-1-startBitInByte+ sig->bitSize + 7) / 8;  // 字节长度
                   /* 抹去信号所占的位 *****************************************************/
                   c << "    data[" << startByte << "] &= 0x";
                   shiftCnt = startBitInByte ;
                   mask = (~((0x01 << (shiftCnt+1))-1))&0xff;
                   if((startBitInByte+1) > sig->bitSize)             // 如果未占满第1个字节
                   {
                       shiftCnt = startBitInByte + 1 - sig->bitSize;
                       mask |= (0x01 << shiftCnt) - 1;
                   }

                   c << QString::number(mask,16) << ";"  << endl;

                   for(ibyte=1; ibyte<byteLength; ibyte++)
                   {
                       c << "    data[" << startByte+ibyte << "] &= 0x";
                       shiftCnt = sig->bitSize - (ibyte-1)*8 - startBitInByte -1;  // 减去前面的
                       shiftCnt = (shiftCnt >= 8)? 8 : shiftCnt;
                       mask = (0x01 << (8 - shiftCnt))-1;
                       c << QString::number(mask,16) << ";"  << endl;
                   }

                   if((startBitInByte+1) > sig->bitSize)             // 如果未占满第1个字节
                   {
                        c << "    data[" << startByte << "] |= ";
                        c << "(" << tempValueStr << " & 0x";
                        mask = (0x01 << sig->bitSize) - 1 ;
                        c << QString::number(mask,16) << ") << ";
                        shiftCnt = startBitInByte + 1 - sig->bitSize;
                        c << shiftCnt << ";" << endl;
                   }
                   else
                   {
                       shiftCnt = sig->bitSize - startBitInByte - 1;
                       mask = (0x01 << (startBitInByte +1))-1;
                       for(ibyte=0; ibyte<byteLength; )
                       {
                           c << "    data[" << startByte + ibyte << "] |= ";
                           c << "(" << tempValueStr << " >> " << shiftCnt << ") & 0x";
                           c << QString::number(mask,16) << ";" << endl;
                           ibyte++;
                           if(shiftCnt >= 8)
                           {
                               shiftCnt -= 8;
                               mask = 0xff;
                           }
                           else
                           {
                               break;
                           }
                       }
                       if(0 != shiftCnt)
                       {
                           mask = (0x01 << shiftCnt) -1;
                           c << "    data[" << startByte + byteLength -1 << "] |= ";
                           c << "(" << tempValueStr << " & 0x";
                           c << QString::number(mask,16) << ") << " << 8 - shiftCnt << ";" << endl;
                       }
                   }
               }
               else  // 小端
               {
                   /*** 抹去信号所占的位 *************************************************************/
                   byteLength = (startBitInByte+ sig->bitSize + 7) / 8;  // 字节长度
                   shiftCnt = startBitInByte;
                   mask = (0x01 << shiftCnt)-1;
                   for(ibyte=0; ibyte<byteLength; )
                   {
                       c << "    data[" << startByte+ibyte << "] &= 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       ibyte++;
                       if((8 - startBitInByte+ibyte*8) <= sig->bitSize)
                       {
                           mask = 0;
                       }
                       else
                       {
                           shiftCnt = (8 - startBitInByte+ibyte*8) - sig->bitSize;
                           shiftCnt = 8 - shiftCnt;
                           mask = ~((0x01 << shiftCnt)-1);
                           mask &= 0xff;
                       }
                   }
                   c << "    data[" << startByte << "] |= ";
                   shiftCnt = 8 - startBitInByte;
                   mask = (0x01 << shiftCnt) -1;
                   c << "(" << tempValueStr << " & 0x";
                   c << QString::number(mask,16) << ") << " << startBitInByte << ";" << endl;
                   for(ibyte=1; ibyte<byteLength; ibyte++)
                   {
                       c << "    data[" << startByte + ibyte << "] |= ";
                       shiftCnt = 8-startBitInByte + (ibyte-1)*8;
                       c << "(" << tempValueStr << " >> " << shiftCnt << ") & 0x";
                       if((shiftCnt + 8) <= sig->bitSize)
                       {
                           mask = 0xff;
                       }
                       else
                       {
                           shiftCnt = sig->bitSize - shiftCnt;
                           mask = (0x01 << shiftCnt) - 1;
                       }
                       c << QString::number(mask,16) << ";" << endl;

                   }

               }
           }
           else if(IeeeFloat == sig->exVauleType)
           {
                c << "    temp_uf.f = (msg->" << sig->name << " - (" << sig->offset << ")) / (" << sig->factor << ");" << endl;
                if(bigEndian == sig->byteOrder)
                {
                    lsbPosInByte = (8 - ((sig->bitSize - (sig->startBit % 8 + 1)) %8)) % 8;
                    if(0 == lsbPosInByte)
                    {
                        c << "    data[" << startByte+3 << "] = temp_uf.u[0];" << endl;
                        c << "    data[" << startByte+2 << "] = temp_uf.u[1];" << endl;
                        c << "    data[" << startByte+1 << "] = temp_uf.u[2];" << endl;
                        c << "    data[" << startByte+0 << "] = temp_uf.u[3];" << endl;
                    }
                    else
                    {
                        shiftCnt = lsbPosInByte;
                        mask = (0x01 << shiftCnt) - 1;
                        c << "    data[" << startByte+4 << "] &= 0x";
                        c << QString::number(mask,16) << ";" << endl;
                        c << "    data[" << startByte+3 << "] = 0;" << endl;
                        c << "    data[" << startByte+2 << "] = 0;" << endl;
                        c << "    data[" << startByte+1 << "] = 0;" << endl;
                        c << "    data[" << startByte << "] &= 0x";
                        c << QString::number(((~mask)&0xff),16) << ";" << endl;


                        c << "    data[" << startByte+4 << "] |= (temp_uf.u[0] << " << shiftCnt << ") & 0x";
                        c << QString::number(((~mask)&0xff),16) << ";" << endl;

                        c << "    data[" << startByte+3 << "] = (temp_uf.u[0] >> " << 8-shiftCnt << ") & 0x";
                        c << QString::number(mask,16) << ";" << endl;
                        c << "    data[" << startByte+3 << "] |= (temp_uf.u[1] << " << shiftCnt << ") & 0x";
                        c << QString::number(((~mask)&0xff),16) << ";" << endl;

                        c << "    data[" << startByte+2 << "] = (temp_uf.u[1] >> " << 8-shiftCnt << ") & 0x";
                        c << QString::number(mask,16) << ";" << endl;
                        c << "    data[" << startByte+2 << "] |= (temp_uf.u[2] << " << shiftCnt << ") & 0x";
                        c << QString::number(((~mask)&0xff),16) << ";" << endl;

                        c << "    data[" << startByte+1 << "] = (temp_uf.u[2] >> " << 8-shiftCnt << ") & 0x";
                        c << QString::number(mask,16) << ";" << endl;
                        c << "    data[" << startByte+1 << "] |= (temp_uf.u[3] << " << shiftCnt << ") & 0x";
                        c << QString::number(((~mask)&0xff),16) << ";" << endl;

                        c << "    data[" << startByte  << "] |= (temp_uf.u[3] >> " <<  8-shiftCnt << ") & 0x";
                        c << QString::number(mask,16) << ";" << endl;

                    }
                }
                else  // 小端
                {
                    if(0 == startBitInByte)
                    {
                        c << "    data[" << startByte+0 << "] = temp_uf.u[0];" << endl;
                        c << "    data[" << startByte+1 << "] = temp_uf.u[1];" << endl;
                        c << "    data[" << startByte+2 << "] = temp_uf.u[2];" << endl;
                        c << "    data[" << startByte+3 << "] = temp_uf.u[3];" << endl;
                    }
                    else
                    {
                        shiftCnt = startBitInByte;
                        mask = (0x01 << shiftCnt) -1;
                        c << "    data[" << startByte << "] &= 0x";
                        c << QString::number(mask,16) << ";" << endl;
                        c << "    data[" << startByte+1 << "] = 0;" << endl;
                        c << "    data[" << startByte+2 << "] = 0;" << endl;
                        c << "    data[" << startByte+3 << "] = 0;" << endl;
                        c << "    data[" << startByte+4 << "] &= 0x";
                        c << QString::number(((~mask)&0xff),16) << ";" << endl;

                        c << "    data[" << startByte << "] |= (temp_uf.u[0] << " << shiftCnt << ") & 0x";
                        c << QString::number(((~mask)&0xff),16) << ";" << endl;

                        c << "    data[" << startByte+1 << "] = (temp_uf.u[0] >> " << 8-shiftCnt << ") & 0x";
                        c << QString::number(mask,16) << ";" << endl;
                        c << "    data[" << startByte+1 << "] |= (temp_uf.u[1] << " << shiftCnt << ") & 0x";
                        c << QString::number(((~mask)&0xff),16) << ";" << endl;

                        c << "    data[" << startByte+2 << "] = (temp_uf.u[1] >> " << 8-shiftCnt << ") & 0x";
                        c << QString::number(mask,16) << ";" << endl;
                        c << "    data[" << startByte+2 << "] |= (temp_uf.u[2] << " << shiftCnt << ") & 0x";
                        c << QString::number(((~mask)&0xff),16) << ";" << endl;

                        c << "    data[" << startByte+3 << "] = (temp_uf.u[2] >> " << 8-shiftCnt << ") & 0x";
                        c << QString::number(mask,16) << ";" << endl;
                        c << "    data[" << startByte+3 << "] |= (temp_uf.u[3] << " << shiftCnt << ") & 0x";
                        c << QString::number(((~mask)&0xff),16) << ";" << endl;

                        c << "    data[" << startByte+4 << "] |= (temp_uf.u[3] >> " << 8-shiftCnt << ") & 0x";
                        c << QString::number(mask,16) << ";" << endl;
                    }

                }

           }
           else   // IEEE double
           {
               c << "    temp_ud.d = (msg->" << sig->name << " - (" << sig->offset << ")) / (" << sig->factor << ");" << endl;
               if(bigEndian == sig->byteOrder)
               {
                   lsbPosInByte = (8 - ((sig->bitSize - (sig->startBit % 8 + 1)) %8)) % 8;
                   if(0 == lsbPosInByte)
                   {
                       c << "    data[" << startByte+7 << "] = temp_ud.u[0];" << endl;
                       c << "    data[" << startByte+6 << "] = temp_ud.u[1];" << endl;
                       c << "    data[" << startByte+5 << "] = temp_ud.u[2];" << endl;
                       c << "    data[" << startByte+4 << "] = temp_ud.u[3];" << endl;
                       c << "    data[" << startByte+3 << "] = temp_ud.u[4];" << endl;
                       c << "    data[" << startByte+2 << "] = temp_ud.u[5];" << endl;
                       c << "    data[" << startByte+1 << "] = temp_ud.u[6];" << endl;
                       c << "    data[" << startByte+0 << "] = temp_ud.u[7];" << endl;
                   }
                   else
                   {
                       shiftCnt = lsbPosInByte;
                       mask = (0x01 << shiftCnt) - 1;
                       c << "    data[" << startByte+8 << "] &= 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       c << "    data[" << startByte+7 << "] = 0;" << endl;
                       c << "    data[" << startByte+6 << "] = 0;" << endl;
                       c << "    data[" << startByte+5 << "] = 0;" << endl;
                       c << "    data[" << startByte+4 << "] = 0;" << endl;
                       c << "    data[" << startByte+3 << "] = 0;" << endl;
                       c << "    data[" << startByte+2 << "] = 0;" << endl;
                       c << "    data[" << startByte+1 << "] = 0;" << endl;
                       c << "    data[" << startByte << "] &= 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+8 << "] |= (temp_ud.u[0] << " << shiftCnt << ") & 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+7 << "] = (temp_ud.u[0] >> " << 8-shiftCnt << ") & 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       c << "    data[" << startByte+7 << "] |= (temp_ud.u[1] << " << shiftCnt << ") & 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+6 << "] = (temp_ud.u[1] >> " << 8-shiftCnt << ") & 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       c << "    data[" << startByte+6 << "] |= (temp_ud.u[2] << " << shiftCnt << ") & 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+5 << "] = (temp_ud.u[2] >> " << 8-shiftCnt << ") & 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       c << "    data[" << startByte+5 << "] |= (temp_ud.u[3] << " << shiftCnt << ") & 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+4 << "] = (temp_ud.u[3] >> " << 8-shiftCnt << ") & 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       c << "    data[" << startByte+4 << "] |= (temp_ud.u[4] << " << shiftCnt << ") & 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+3 << "] = (temp_ud.u[4] >> " << 8-shiftCnt << ") & 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       c << "    data[" << startByte+3 << "] |= (temp_ud.u[5] << " << shiftCnt << ") & 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+2 << "] = (temp_ud.u[5] >> " << 8-shiftCnt << ") & 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       c << "    data[" << startByte+2 << "] |= (temp_ud.u[6] << " << shiftCnt << ") & 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+1 << "] = (temp_ud.u[6] >> " << 8-shiftCnt << ") & 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       c << "    data[" << startByte+1 << "] |= (temp_ud.u[7] << " << shiftCnt << ") & 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+0 << "] |= (temp_ud.u[7] >> " << 8-shiftCnt << ") & 0x";
                       c << QString::number(mask,16) << ";" << endl;



                   }

               }
               else  // 小端
               {
                   if(0 == startBitInByte)
                   {
                       c << "    data[" << startByte+0 << "] = temp_ud.u[0];" << endl;
                       c << "    data[" << startByte+1 << "] = temp_ud.u[1];" << endl;
                       c << "    data[" << startByte+2 << "] = temp_ud.u[2];" << endl;
                       c << "    data[" << startByte+3 << "] = temp_ud.u[3];" << endl;
                       c << "    data[" << startByte+4 << "] = temp_ud.u[4];" << endl;
                       c << "    data[" << startByte+5 << "] = temp_ud.u[5];" << endl;
                       c << "    data[" << startByte+6 << "] = temp_ud.u[6];" << endl;
                       c << "    data[" << startByte+7 << "] = temp_ud.u[7];" << endl;
                   }
                   else
                   {
                       shiftCnt = startBitInByte;
                       mask = (0x01 << shiftCnt) -1;
                       c << "    data[" << startByte << "] &= 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       c << "    data[" << startByte+1 << "] = 0;" << endl;
                       c << "    data[" << startByte+2 << "] = 0;" << endl;
                       c << "    data[" << startByte+3 << "] = 0;" << endl;
                       c << "    data[" << startByte+4 << "] = 0;" << endl;
                       c << "    data[" << startByte+5 << "] = 0;" << endl;
                       c << "    data[" << startByte+6 << "] = 0;" << endl;
                       c << "    data[" << startByte+7 << "] = 0;" << endl;
                       c << "    data[" << startByte+8 << "] &= 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte << "] |= (temp_ud.u[0] << " << shiftCnt << ") & 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+1 << "] = (temp_ud.u[0] >> " << 8-shiftCnt << ") & 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       c << "    data[" << startByte+1 << "] |= (temp_ud.u[1] << " << shiftCnt << ") & 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+2 << "] = (temp_ud.u[1] >> " << 8-shiftCnt << ") & 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       c << "    data[" << startByte+2 << "] |= (temp_ud.u[2] << " << shiftCnt << ") & 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+3 << "] = (temp_ud.u[2] >> " << 8-shiftCnt << ") & 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       c << "    data[" << startByte+3 << "] |= (temp_ud.u[3] << " << shiftCnt << ") & 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+4 << "] = (temp_ud.u[3] >> " << 8-shiftCnt << ") & 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       c << "    data[" << startByte+4 << "] |= (temp_ud.u[4] << " << shiftCnt << ") & 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+5 << "] = (temp_ud.u[4] >> " << 8-shiftCnt << ") & 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       c << "    data[" << startByte+5 << "] |= (temp_ud.u[5] << " << shiftCnt << ") & 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+6 << "] = (temp_ud.u[5] >> " << 8-shiftCnt << ") & 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       c << "    data[" << startByte+6 << "] |= (temp_ud.u[6] << " << shiftCnt << ") & 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+7 << "] = (temp_ud.u[6] >> " << 8-shiftCnt << ") & 0x";
                       c << QString::number(mask,16) << ";" << endl;
                       c << "    data[" << startByte+7 << "] |= (temp_ud.u[7] << " << shiftCnt << ") & 0x";
                       c << QString::number(((~mask)&0xff),16) << ";" << endl;

                       c << "    data[" << startByte+8 << "] |= (temp_ud.u[8] >> " << 8-shiftCnt << ") & 0x";
                       c << QString::number(mask,16) << ";" << endl;

                   }

               }

           }
        }
        c << "}" << endl;

    }

    c << "static int64_t u2s_64(uint64_t data,uint8_t n)      " << endl;
    c << "{                                                   " << endl;
    c << "    int64_t  ret;                                   " << endl;
    c << "    uint64_t bit_mask = 1 << (n-1);                 " << endl;
    c << "    if(data & bit_mask)                             " << endl;
    c << "    {                                               " << endl;
    c << "        ret = (int64_t)(data | (~(bit_mask - 1)));  " << endl;
    c << "    }                                               " << endl;
    c << "    else                                            " << endl;
    c << "    {                                               " << endl;
    c << "        ret = (int64_t)data;                        " << endl;
    c << "    }                                               " << endl;
    c << "    return ret;                                     " << endl;
    c << "}                                                   " << endl;
    c << endl;

    c << "static int32_t u2s_32(uint32_t data,uint8_t n)      " << endl;
    c << "{                                                   " << endl;
    c << "    int32_t  ret;                                   " << endl;
    c << "    uint32_t bit_mask = 1 << (n-1);                 " << endl;
    c << "    if(data & bit_mask)                             " << endl;
    c << "    {                                               " << endl;
    c << "        ret = (int32_t)(data | (~(bit_mask - 1)));  " << endl;
    c << "    }                                               " << endl;
    c << "    else                                            " << endl;
    c << "    {                                               " << endl;
    c << "        ret = (int32_t)data;                        " << endl;
    c << "    }                                               " << endl;
    c << "    return ret;                                     " << endl;
    c << "}                                                   " << endl;
    c << endl;

    c << "static int16_t u2s_16(uint16_t data,uint8_t n)      " << endl;
    c << "{                                                   " << endl;
    c << "    int16_t  ret;                                   " << endl;
    c << "    uint16_t bit_mask = 1 << (n-1);                 " << endl;
    c << "    if(data & bit_mask)                             " << endl;
    c << "    {                                               " << endl;
    c << "        ret = (int16_t)(data | (~(bit_mask - 1)));  " << endl;
    c << "    }                                               " << endl;
    c << "    else                                            " << endl;
    c << "    {                                               " << endl;
    c << "        ret = (int16_t)data;                        " << endl;
    c << "    }                                               " << endl;
    c << "    return ret;                                     " << endl;
    c << "}                                                   " << endl;
    c << endl;

    c << "static int8_t u2s_8(uint8_t data,uint8_t n)         " << endl;
    c << "{                                                   " << endl;
    c << "    int8_t  ret;                                    " << endl;
    c << "    uint8_t bit_mask = 1 << (n-1);                  " << endl;
    c << "    if(data & bit_mask)                             " << endl;
    c << "    {                                               " << endl;
    c << "        ret = (int8_t)(data | (~(bit_mask - 1)));   " << endl;
    c << "    }                                               " << endl;
    c << "    else                                            " << endl;
    c << "    {                                               " << endl;
    c << "        ret = (int8_t)data;                         " << endl;
    c << "    }                                               " << endl;
    c << "    return ret;                                     " << endl;
    c << "}                                                   " << endl;

    cfile.close();
}

bool coderClass::isFloatValue(double val)
{
    return (fabs(val - floor(val)) > 0.00000001)? true : false;
}

qint64 coderClass::max(qint64 &a, qint64 &b)
{
    return (a >= b)? a : b;
}
